package Controller;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DAOFactory;
import DAO.HolidayDAO;
import DAO.RankDAO;
import Model.*;

/**
 * Servlet implementation class HolidayDelete
 */
@WebServlet("/HolidayDelete")
public class HolidayDelete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HolidayDelete() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Authenticate(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Authenticate(request,response);
	}
	
	protected void Authenticate (HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		//Authenticate before starting process
		if ( LoginAdmin.CheckObject(request, response) ) {
			doProcess(request,response);
		} else {	//Otherwise Authentication Object not found
			response.sendRedirect(LoginAdmin.LOGIN_CONTROL);
		}
	}
	
	protected void doProcess(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		if ( LoginAdmin.CheckObject(request, response) ) {
			String holidayId = request.getParameter("HolidayId");
			if (holidayId != null) {				//If HolidayId found
				if ( holidayId.matches("[0-9]+") )	//If HolidayId is number
				{
					int holidayInt = Integer.parseInt(holidayId);
					HolidayDAO holidayDAO = 			//get DAO
						DAOFactory.getInstance().getHolidayDAO();
					try{
						holidayDAO.deleteHoliday(holidayInt);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			response.sendRedirect("HolidayList");	//Redirect here
		} else {	//Otherwise Authentication Object not found
			response.sendRedirect(LoginAdmin.LOGIN_CONTROL);
		}
	}
}
